/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASWingUtils;
import org.aswing.border.EmptyBorder;
import org.aswing.BorderLayout;
import org.aswing.Container;
import org.aswing.geom.Point;
import org.aswing.Insets;
import org.aswing.JFrame;

import com.bourre.core.HashCodeFactory;

import fever.app.accelerator.Keyboard;
import fever.app.accelerator.ShortcutLocator;
import fever.app.accelerator.ShortcutMap;
import fever.display.ModalScreen;

import fvaswing.FvAsWing;
import fvaswing.visual.FvViewHelper;

/**
 * Defines basic <strong>Pixlib View</strong> using 
 * {@link fvaswing.visual.FvViewHelper} and a JFrame as main 
 * container. 
 * 
 * <p>Feel free to create subclasses for custom frame behaviour.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.visual.FvAbstractFrameView extends FvViewHelper
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _frame : JFrame;
	private var _modal : ModalScreen;	private var _isModal : Boolean;
	private var _keymap : ShortcutMap;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns concrete view with typecast
	 * 
	 * <p>Must be override in subclass for type checking.
	 * 
	 * @return Component here
	 */
	public function getView() : JFrame
	{
		return JFrame( view );	
	}
	
	/**
	 * Returns frame content pane.
	 * 
	 * <p>Use to clearly append childs into frame. 
	 */
	public function getContentPane( ) : Container
	{
		return _frame.getContentPane();
	}
	
	/**
	 * Defines frame caption.
	 */
	public function setTitle( str : String ) : Void
	{
		_frame.setTitle( str );
	}
	
	/**
	 * Returns frame caption.
	 */
	public function getTitle() : String
	{
		return _frame.getTitle( );	
	}
	
	/**
	 * setLocation(x:Number, y:Number)<br>
	 * setLocation(p:Point)
	 * <p>
	 * Set the component's location, if it is diffs from old location, invalidate it to wait validate.
	 * The top-left corner of the new location is specified by the x and y parameters 
	 * in the coordinate space of this component's parent.
	 */
	public function setLocation() : Void
	{
		_frame.setLocation.apply( _frame, arguments );
	}
	
	/**
	 * getLocation(p:Point)<br>
	 * getLocation()
	 * <p>
	 * Stores the location value of this component into "return value" p and returns p. 
	 * If p is null or undefined a new Point object is allocated. 
	 * @param p the return value, modified to the component's location.
	 */
	public function getLocation( p  : Point ) : Point
	{
		return _frame.getLocation( p );	
	}
	
	/**
	 * Returns key mapping map name.
	 */
	public function getKeyMapName() : String
	{
		return _keymap.getName();	
	}
	
	/**
	 * Opens window
	 */
	public function open() : Void
	{
		_loadKeyMap();
		
		if( _isModal ) 
		{
			if( _frame.getDefaultCloseOperation() == JFrame.DISPOSE_ON_CLOSE )
			{
				_frame.changeOwner( _getModalTarget() );
			}
			_modal.show();
		}
		
		_frame.setState( JFrame.NORMAL );
		_frame.show();
	}
	
	/**
	 * Closes current windows.
	 * 
	 * <p>Closing behaviour depending of 
	 * {@link #setCloseOperation()} method
	 */
	public function close() : Void
	{
		_keymap.unload();
		
		if( _isModal ) 
		{
			if( _frame.getDefaultCloseOperation() == JFrame.DISPOSE_ON_CLOSE )
				_modal.dispose();
			else _modal.hide();
		}
		
		_frame.tryToClose();
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FvAbstractFrameView( id : String, modal : Boolean )
	{
		super( _createUI( modal ), id );
		
		_keymap = ShortcutLocator.getMap( 'f' + HashCodeFactory.getKey( this ) );
		_keymap.registerEvent( Keyboard.onKeyESCAPE, this, close );
		
		_configureFrame();
	}
	
	/**
	 * Creates JFrame component.
	 */
	private function _createUI( modal : Boolean ) : JFrame
	{
		_isModal = ( modal == true ) ? true : false;
		
		var ft : MovieClip = ( _isModal ) ? _getModalTarget() : _getFrameTarget();
		_frame = new JFrame( ft, '', false );
		return _frame;	
	}
	
	private function _getModalTarget() : MovieClip
	{
		_modal.dispose();
		_modal = new ModalScreen();
		
		return _modal.getModalTarget();	
	}
	
	private function _getFrameTarget() : MovieClip
	{
		return FvAsWing.getInstance().getRootMovieClip();	
	}
	
	/**
	 * Creates and configures Jframe.
	 */
	private function _configureFrame() : Void
	{
		_frame.setResizable( true );
		_frame.setClosable( true );
		_frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
		_frame.getContentPane().setBorder( new EmptyBorder( null, new Insets( 5, 5, 5, 5 ) ) );
		_frame.getContentPane().setLayout( new BorderLayout( 5, 5 ) );		_frame.addEventListener( JFrame.ON_WINDOW_CLOSING, _close, this );	}
	
	private function _loadKeyMap() : Void
	{
		_keymap.load();
	}
	
	/**
	 * Centers JFrame on Stage
	 */
	private function _layout() : Void
	{
		var location : Point = ASWingUtils.getScreenCenterPosition();
		location.x -= _frame.getWidth()/2;
		location.y -= _frame.getHeight()/2;
		_frame.setLocation( location );
	}
	
	/**
	 * Defined intermediate close callback to allow developer to override 
	 * to this method to disable default closing behaviour. 
	 */
	private function _close() : Void
	{
		close();
	}
}